package com.longxi.tianjintianbao.notification;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.ui.activity.goodstraffic.SignGoodsReleaseActivity;
import com.longxi.tianjintianbao.ui.activity.main.MainActivity;
import com.longxi.tianjintianbao.ui.activity.oa.ChangeFormalDetailActivity;
import com.longxi.tianjintianbao.ui.activity.oa.CopeWithDetailActivity;
import com.longxi.tianjintianbao.ui.activity.oa.CostApplyDetailActivity;
import com.longxi.tianjintianbao.ui.activity.quality.QualityClassListActivity;
import com.longxi.tianjintianbao.ui.activity.rectification.RectificationDetailActivity;
import com.longxi.tianjintianbao.ui.activity.repair.RepairDetailActivity;
import com.longxi.tianjintianbao.ui.activity.task.TaskDetailActivity;
import com.longxi.tianjintianbao.utils.MyUtils;


public class MyNotification {
    private String TAG = "MyNotification";

    private static MyNotification mMyNotification;
    private static NotificationManager mNotifyManager;

    public static MyNotification getMyNotificationInstance(){
        if(mMyNotification == null){
            synchronized (MyNotification.class){
                if(mMyNotification == null){
                    mMyNotification = new MyNotification();
                }
            }
        }
        return mMyNotification;
    }

    public static NotificationManager getNotifyManagerInstance(){
        if(mNotifyManager == null){
            synchronized (NotificationManager.class){
                if(mNotifyManager == null){
                    mNotifyManager = (NotificationManager) MyApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
                }
            }
        }
        return mNotifyManager;
    }

    /**
     * 发送一个点击跳转到MainActivity的消息
     */
    public void sendSimplestNotificationWithAction(String topic, String message, String activity, String jump_value) {
        Intent mainIntent = new Intent(MyApplication.getInstance(), MainActivity.class);

        //获取PendingIntent
        switch (activity) {
            case "MainActivity":
                mainIntent = new Intent(MyApplication.getInstance(), MainActivity.class);
//                mainIntent.putExtra("url",jump_value);
                break;
        }

        PendingIntent mainPendingIntent = PendingIntent.getActivity(MyApplication.getInstance(), 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        //创建 Notification.Builder 对象
        NotificationCompat.Builder builder = new NotificationCompat.Builder(MyApplication.getInstance())
                .setSmallIcon(R.mipmap.head_icon)
                //点击通知后自动清除
                .setAutoCancel(true)
                .setContentTitle(topic)
                .setContentText(message)
                .setContentIntent(mainPendingIntent);
        //发送通知
        getNotifyManagerInstance().notify(3, builder.build());
    }

    /**
     * 点击跳转到相应页面 未整合
     * @param context
     * @param billdid
     * @param billtype
     */
    public void jump(Context context, String billdid, int billtype){
        Intent mainIntent = null;
        switch (billtype) {
            case 1://任务单
                mainIntent = new Intent(context, TaskDetailActivity.class);
                mainIntent.putExtra(AppConst.BILLID,billdid);
                mainIntent.putExtra(AppConst.STATE,false);
                break;
            case 2://报修单
                mainIntent = new Intent(context, RepairDetailActivity.class);
                mainIntent.putExtra(AppConst.BILLID,billdid);
                break;
            case 3://oa
                toApplyActivity(context, billdid);
                return;
            case 4://品质检查
                mainIntent = new Intent(context, QualityClassListActivity.class);
                mainIntent.putExtra(AppConst.BILLID,billdid);
                mainIntent.putExtra(AppConst.BUILDNAME,"品质检查");
                break;
            case 5://品质整改
                mainIntent = new Intent(context, RectificationDetailActivity.class);
                mainIntent.putExtra(AppConst.BILLID,billdid);
                break;
            case 6://物品放行
                mainIntent = new Intent(context, SignGoodsReleaseActivity.class);
                mainIntent.putExtra(AppConst.BILLID,billdid);
                mainIntent.putExtra(AppConst.STATE,"0");
                break;
        }
        if(mainIntent == null){
            MyUtils.showToast("跳转错误");
            return;
        }

        mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
        context.startActivity(mainIntent);
    }

    /**
     * oa单比较复杂
     * @param context
     * @param billid
     */
    public void toApplyActivity(Context context, String billid) {
        String[] strs=billid.replace(" ", "").split(",");

        Intent mainIntent = null;
        switch (strs[1]) {
            case "13":
                mainIntent = new Intent(context, CostApplyDetailActivity.class);
                mainIntent.putExtra(AppConst.BILLID,strs[0]);
                mainIntent.putExtra(AppConst.STATE,"0");
                break;
            case "280":
                mainIntent = new Intent(context, CopeWithDetailActivity.class);
                mainIntent.putExtra(AppConst.BILLID,strs[0]);
                mainIntent.putExtra(AppConst.STATE,"0");
                break;
            case "19":
                mainIntent = new Intent(context, ChangeFormalDetailActivity.class);
                mainIntent.putExtra(AppConst.BILLID,strs[0]);
                mainIntent.putExtra(AppConst.STATE,"0");
                break;
        }

        mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
        context.startActivity(mainIntent);
    }
}
